<?php
namespace Biz\service\designerAward;

use Biz\model\designerAward\Sign as mSign;
use Biz\model\designerAward\User as mDesignerAwardUser;
use Biz\tools\ShopArea;
use Doraemon\model\Article as mArticle;
use Doraemon\model\ArticleHouse as mArticleHouse;
use Doraemon\model\Member;
use Doraemon\service\User as sUser;
use Doraemon\model\DesignerCompetitionProduct;
use Doraemon\tools\ImgUrl;
use Medoo\Medoo;

/**
 * @deprecated
 */
class AdmDesignerAward
{

    /*2019年9月11日·华东分赛区·上海
    华东赛区含：上海市、江苏省、浙江省、安徽省、江西省、
    2019年9月21日·西南分赛区·成都
    西南赛区含：重庆市、四川省、贵州省、云南省、陕西省、甘肃省、青海省、宁夏回族自治区、新疆维吾尔自治区、西藏自治区
    2019年9月22日·华南分赛区·深圳
    华南赛区包含：湖南省、广东省、海南省、福建省、台湾省、广西壮族自治区、香港特别行政区、澳门特别行政区
    2019年10月21日·华北分赛区·北京
    华北赛区含：北京市，天津市，河北省、河南省、山东省、山西省、湖北省、黑龙江省、吉林省、辽宁省和内蒙古自治区*/
    const USER_AREA_EAST = 1;
    const USER_AREA_WEST = 2;
    const USER_AREA_SOUTH = 3;
    const USER_AREA_NORTH = 4;
    const USER_AREA = [
        self::USER_AREA_EAST => '华东分赛区·上海',
        self::USER_AREA_WEST => '西南分赛区·成都',
        self::USER_AREA_SOUTH => '华南分赛区·深圳',
        self::USER_AREA_NORTH => '华北分赛区·北京',
    ];
    const ADM_USER_TYPE = [
        Member::USER_COMMON => '住友',
        Member::AUTH_USER_DESIGNER => '设计师',
        Member::AUTH_USER_UNAUTH_DESIGNER => '未认证',
        Member::AUTH_USER_V => 'V认证',
    ];
    const ADM_CHANNEL_MAP = [
        mDesignerAwardUser::USER_TYPE_OWNER => '业主',
        mDesignerAwardUser::USER_TYPE_DESIGNER => '设计师',
    ];

    const AREA_MAP = [
        self::USER_AREA_EAST => [
            "310000", "320000", "330000", "340000", "360000"
        ],
        self::USER_AREA_WEST => [
            "500000", "510000", "520000", "530000", "610000", "620000", "630000", "640000",
            "650000", "540000"
        ],
        self::USER_AREA_SOUTH => [
            "430000", "440000", "460000", "350000", "710000", "450000", "810000", "820000"
        ],
        self::USER_AREA_NORTH => [
            "110000", "120000", "130000", "410000", "370000", "140000", "420000", "230000",
            "220000", "210000", "150000"
        ],
    ];

    const USER_TYPE_MAP = [
        Member::USER_COMMON => '住友',
        Member::AUTH_USER_DESIGNER => '设计师',
        Member::AUTH_USER_UNAUTH_DESIGNER => '未认证',
        Member::AUTH_USER_V => 'V认证',
    ];

    public static $topMap = [
        "110000"=> "北京市", "120000"=> "天津市", "130000"=> "河北省", "140000"=> "山西省", "150000"=> "内蒙古自治区",
        "210000"=> "辽宁省", "220000"=> "吉林省", "230000"=> "黑龙江省",
        "310000"=> "上海市", "320000"=> "江苏省", "330000"=> "浙江省", "340000"=> "安徽省", "350000"=> "福建省", "360000"=> "江西省", "370000"=> "山东省",
        "410000"=> "河南省", "420000"=> "湖北省", "430000"=> "湖南省", "440000"=> "广东省", "450000"=> "广西壮族自治区", "460000"=> "海南省",
        "500000"=> "重庆市", "510000"=> "四川省", "520000"=> "贵州省", "530000"=> "云南省", "540000"=> "西藏自治区",
        "610000"=> "陕西省", "620000"=> "甘肃省", "630000"=> "青海省", "640000"=> "宁夏回族自治区", "650000"=> "新疆维吾尔自治区",
        "710000"=> "台湾省",
        "810000"=> "香港特别行政区", "820000"=> "澳门特别行政区"
    ];

    public static function getArea($areasArr)
    {
        $find = null;
        if (isset($areasArr[0])) {
            $top = $areasArr[0];
            foreach (self::AREA_MAP as $area => $map) {
                if (in_array($top, $map)) {
                    $find = $area;
                    break;
                }
            }
        }
        return $find;
    }

    public static function oldDataExists($aids)
    {
        //获取历史报名数据
        $rs = DesignerCompetitionProduct::select('aid', ['aid' => $aids]);
        return empty($rs) ? [] : $rs;
    }

    public static function oldData()
    {
        //获取历史报名数据
        $rs = DesignerCompetitionProduct::select('*', ['LIMIT' => [0, 10], 'ORDER' => ['id' => 'DESC']]);
        //获取文章id集合
        $aids = empty($rs) ? [] : array_column($rs, 'aid');
        $aids = ["0000ds801000d1ap"];

        $articles = mArticle::getAllArticles($aids);


///Activity/Designer/productList2018/menuid/860.html
        var_dump($aids, $articles);
    }

    /**
     * @desc 后台获取分页获取wiki信息
     * @param $condition array 搜索条件
     * @return array
     */
    public static function admList($condition, $limit)
    {
        if (!isset($condition['ORDER'])) {
            $condition['ORDER'] = [
                'add_time' => 'DESC'
            ];
        }

        $condition['LIMIT'] = $limit;

        $rs = mSign::getAllByWhere($condition);

        $list = [];
        if ($rs) {
            $aids = array_column($rs, 'aid');
            $uids = array_column($rs, 'uid');
            $uids = array_flip(array_flip($uids));
            //查询文章信息
            $articles = mArticle::getAllArticles($aids);
            $articles = empty($articles) ? [] : array_column($articles, null, 'aid');
            $houses = mArticleHouse::getArticleHouseByIds($aids);
            $houses = empty($houses) ? [] : array_column($houses, null, 'aid');

            //报名信息
            $awardInfo = mDesignerAwardUser::getAllByWhere(['uid' => $uids]);
            $awardMap = empty($awardInfo) ? [] : array_column($awardInfo, null, 'uid');
            $changeIds = [];
            foreach ($rs as $v) {
                $aid = $v['aid'];
                //查询品牌账号昵称
                try {
                    $userInfo = $v['uid'] > 0 ? sUser::getUser($v['uid']) : [];
                } catch (\Exception $e) {
                    $userInfo = [];
                }
                $user_nick = isset($userInfo['nick']) ? $userInfo['nick'] : '';
                $user_type = isset($userInfo['type']) && isset(self::USER_TYPE_MAP[$userInfo['type']]) ? self::USER_TYPE_MAP[$userInfo['type']] : '未知';
                $tempAwardInfo = isset($awardMap[$v['uid']]) ? $awardMap[$v['uid']] : [];
                $areas = isset($tempAwardInfo['areas']) ? $tempAwardInfo['areas'] : "";
                $areasArr = strlen($areas) ? explode(',', $areas) : [];
                $area = self::getArea($areasArr);
                $areaDesc = isset(self::USER_AREA[$area]) ? self::USER_AREA[$area] : '';
                if (!empty($tempAwardInfo) && ($area != $tempAwardInfo['area'])) {
                    $changeIds[$tempAwardInfo['id']] = $area;
                }

                $list[] = [
                    'id' => $v['id'],
                    'uid' => $v['uid'],
                    'desc' => $v['descrip'],
                    'types' => isset($tempAwardInfo['types']) && isset(mDesignerAwardUser::USER_TYPE[$tempAwardInfo['types']]) ? mDesignerAwardUser::USER_TYPE[$tempAwardInfo['types']] : '未知报名通道',
                    'areas' => !empty($areasArr)  ? ShopArea::locationId2Name($areas) : '未知报名地区',
                    'areaDesc' => !empty($areaDesc)  ? $areaDesc : '未知赛区',
                    'is_soft' => $v['crown_award'] > 0 ? '是' : '否',
                    'aid' => $aid,
                    'house' => mSign::USER_HOUSE[$v['house']] ?? '未定义类型',
                    'user_nick' => $user_nick,
                    'user_type' => $user_type,
                    'add_time' => $v['add_time'],
                    'check_status' => $v['check_status'],
                    'status_desc' => mSign::CHECK_STATUS_MAP[$v['check_status']] ?? '未知状态',
                    'memo' => $v['memo'],
                    'quality' => isset($articles[$aid]) ? $articles[$aid]['admin_score'] : '-',
                    'house_info' => [
                        'title' => isset($articles[$aid]) ? $articles[$aid]['title'] : '',
                        'cover_pic' => isset($articles[$aid]) && $articles[$aid]['cover_pic_id'] ? ImgUrl::getUrl(ImgUrl::APPTYPE_APP, $articles[$aid]['cover_pic_id'], ImgUrl::IMAGE_SIZE_SQ_PHONE) : '',
                        'house_size' => isset($houses[$aid]) ? $houses[$aid]['house_size'] : '',
//                        'h5_url' => HhzEnv::detectShopDomain() . 'h5_article/?article_id=' . $aid,
                        'h5_url' => 'https://m.haohaozhu.com/h5_article/?article_id=' . $aid,
                    ],

                ];
            }

            //更新数据
            if (!empty($changeIds)) {
                foreach ($changeIds as $id => $area) {
                    mDesignerAwardUser::updateData(['area' => $area], ['id' => $id]);
                }
            }
        }

        if (isset($condition['LIMIT'])) {
            unset($condition['LIMIT']);
        }
        if (isset($condition['ORDER'])) {
            unset($condition['ORDER']);
        }
        $total = mSign::count($condition);
        return compact('list', 'total');
    }

    public static function admUserCount($condition = [])
    {
        $rs = mSign::get(['count' => Medoo::raw("count(DISTINCT `uid`)")], $condition);
        return empty($rs['count']) ? 0 : $rs['count'];
    }
}